package com.egoo.ticket.server.config.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.service.EurmDataSyncService;
import com.egoo.ticket.server.service.MqBusinessExecuteService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: victor_tang
 * @Date: 2020/8/9 15:23
 * @Description: 工作模式消费者
 */
@Component
@Slf4j
public class WorkReceiveListener {

    @Autowired
    MqBusinessExecuteService mqBusinessExecuteService;
    @Autowired
	EurmDataSyncService eurmDataSyncService;

    /**
     * TODO 触发器业务执行监听器
     *
     * @param msgContent 只包含发送的消息
     * @param channel    通道信息
     * @param message    附加的参数信息
     * @author: victor_tang
     * @createtime: 2020/8/9 17:07
     * @return: void
     */
    @RabbitListener(queues = "#{triggerQueue.name}")
    public void triggerListener(String msgContent, Channel channel,
                                Message message) {
        log.info("work模式" + message.getMessageProperties().getConsumerQueue() +
                "队列收到的消息内容为：" + msgContent);
//        log.info("work模式channel通道信息为：" + channel);
//        log.info("work模式附加的参数信息为：" + message);
        //处理业务
        if (StringUtils.isEmpty(msgContent)) {
            log.error("收到的触发器队列mq内容为空！");
            return;
        }
        mqBusinessExecuteService.mqExecuteTrigger(msgContent);
    }

    /**
     * TODO 系统消息业务执行监听器
     *
     * @param msgContent 1
     * @param channel    2
     * @param message    3
     * @author: victor_tang
     * @createtime: 2020/8/28 10:40
     * @return: void
     */
    @RabbitListener(queues = "#{sysMsgQueue.name}")
    public void sysMsgListener(String msgContent, Channel channel,
                               Message message) {
        log.info("work模式" + message.getMessageProperties().getConsumerQueue() +
                "队列收到的消息内容为：" + msgContent);

        //处理业务
        if (StringUtils.isEmpty(msgContent)) {
            log.error("收到的系统消息队列mq内容为空！");
            return;
        }
        String[] split = msgContent.split("-");
        String workOrderId = split[0];
        String msgSign = split[1];
        mqBusinessExecuteService.mqExecuteSysMsg(workOrderId, msgSign);
    }

    /**
     * TODO 短信业务执行监听器
     *
     * @param msgContent 1
     * @param channel    2
     * @param message    3
     * @author: victor_tang
     * @createtime: 2020/8/28 10:41
     * @return: void
     */
    @RabbitListener(queues = "#{shortMsgQueue.name}")
	public void shortMsgListener(String msgContent, Channel channel,
								 Message message) {
		log.info("work模式" + message.getMessageProperties().getConsumerQueue() +
				"队列收到的消息内容为：" + msgContent);
		//处理业务
		if (StringUtils.isEmpty(msgContent)) {
			log.error("收到的短信队列mq内容为空！");
			return;
		}
		JSONObject jsonObject = JSON.parseObject(msgContent);
		String workOrderId = jsonObject.getString(ConstantCode.MsgParam.WORK_ORDER_ID);
		String msgSign = jsonObject.getString(ConstantCode.MsgParam.MSG_TARGET_SIGN);
		String msgType = jsonObject.getString(ConstantCode.MsgParam.MSG_TYPE);
		mqBusinessExecuteService.mqExecuteMsg(workOrderId, msgSign,msgType);

	}

	/**
	 * @Author: Donny_dong
	 * @Description: mq通用队列
	 * @Param:
	 * @return:
	 * @Date: 2020/11/2
	 */
	@RabbitListener(queues = "#{generalQueue.name}")
	public void generalListener(String msgContent, Channel channel,
								Message message) {
		log.info("work模式" + message.getMessageProperties().getConsumerQueue() +
				"队列收到的消息内容为：" + msgContent);
		//处理业务
		if (StringUtils.isEmpty(msgContent)) {
			log.error("收到的短信队列mq内容为空！");
			return;
		}
		JSONObject jsonObject = JSON.parseObject(msgContent);
		String workOrderId = jsonObject.getString(ConstantCode.MsgParam.WORK_ORDER_ID);
		String msgNotifyType = jsonObject.getString(ConstantCode.MsgParam.MSG_NOTIFY_TYPE);

		if (!StringUtils.isEmpty(workOrderId) && !StringUtils.isEmpty(msgNotifyType)){
			mqBusinessExecuteService.mqExecuteGeneralMsg(workOrderId,msgNotifyType);
		}
//		log.info("mq发送消息的参数不完整："+workOrderId+"--------"+msgNotifyType);
		return;

	}
	/**
	 * @Author: Donny_dong
	 * @Description: mq通用队列
	 * @Param:
	 * @return:
	 * @Date: 2020/11/2
	 */
	@RabbitListener(queues = "#{syneurmdataQueue.name}")
	public void syneurmdataListener(String msgContent, Channel channel,
								Message message) {
		log.info("work模式" + message.getMessageProperties().getConsumerQueue() +
				"队列收到的消息内容为：" + msgContent);
		//处理业务
		if (StringUtils.isEmpty(msgContent)) {
			log.error("收到的同步内管数据队列mq内容为空！");
			return;
		}
		JSONObject jsonObject = JSON.parseObject(msgContent);
		String dataType = jsonObject.getString("dataType");
		if (ConstantCode.SynEurmDataType.USER.equals(dataType)) {
			// 同步用户数据
			eurmDataSyncService.batchSynUserData(jsonObject);
		}
		else if (ConstantCode.SynEurmDataType.ROLE.equals(dataType)) {
			// 同步角色数据
			eurmDataSyncService.batchSynRoleData(jsonObject);
		}
		else if (ConstantCode.SynEurmDataType.DEPT_GROUP.equals(dataType)) {
			// 同步行政组数据
			eurmDataSyncService.batchSynDeptGroupData(jsonObject);
		}
		else {
			log.error(dataType+"类型数据，工单系统无法识别！");
		}
	}

}
